package com.ruoyi.quartz.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil extends JsonSerializer<Date> {

	/**
	 * 根据时间获取随机编号
	 * @return
	 */
	public static String getRandomNumWithDate(Integer alarmType,String code) {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd");
		String nowStr = formatter.format(currentTime);
		String type = alarmType.toString().length() == 1 ? "0"+ alarmType : alarmType.toString();
		String num = subStr(code,nowStr);
		return nowStr + type + num;
	}

	public static String subStr(String code,String nowStr) {
		String str = "00001";
		if(code == null || "".equals(code)) {
			return str;
		}
		String time = code.substring(0, 6);
		String number = code.substring(8);
		if(time.equals(nowStr)) {
			Integer num = Integer.parseInt(number);
			int value = 100000 + num + 1;
			str = String.valueOf(value).substring(1);
		}
		return str;
	}

	/**
	 * 获取当前日期
	 *
	 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate(String format) {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(currentTime);
		return dateString;
	}
    /**
	 * 获取当前日期-多少天时间
	 *
	 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDateOFF(String format,int day) {
		Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-day);
		Date currentTime =cal.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 获取当前日期-多少天时间
	 *
	 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
	 */
	public static Date getDateOFF(int day) {
		Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-day);
		Date currentTime =cal.getTime();
		return currentTime;
	}

	@SuppressWarnings("static-access")
	public static String getDate(Integer type) {
		// 获取今日日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//Date date = new Date();
		// 获取最近指定天数日期
		Calendar calendar = new GregorianCalendar();

		//calendar.setTime(date);

		calendar.add(calendar.DATE, type);

		String date2 = sdf.format(calendar.getTime());
		return date2;

	}

	   @SuppressWarnings("static-access")
		public static String getMonth(Integer type) {
			// 获取今日日期
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			//Date date = new Date();
			// 获取最近指定天数日期
			Calendar calendar = new GregorianCalendar();

			//calendar.setTime(date);

			calendar.add(calendar.MONTH, type);

			String date2 = sdf.format(calendar.getTime());
			return date2;
		}

	/**
	 * 日期格式化转String
	 * @param date
	 * @return
	 */
	public static String DateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
    }


	/**
	 * String 转date
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date StringToDateInF(String date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
    }

	/**
	 * String 转date
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date StringToDate(String date) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.parse(date);
    }






	@Override
	public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		 String formattedDate = sdf.format(value);
		 gen.writeString(formattedDate);

	}





	/**
	 * 时间戳转换成日期格式字符串
	 * @param seconds 精确到秒的字符串
	 * @param formatStr
	 * @return
	 */
	public static String timeStamp2Date(String seconds,String format) {

		if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
			return "";
		}
		if(format == null || format.isEmpty()){
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(Long.valueOf(seconds+"000")));
	}
	/**
	 * 日期格式字符串转换成时间戳
	 * @param date 字符串日期
	 * @param format 如：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String date2TimeStamp(String date_str,String format){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return String.valueOf(sdf.parse(date_str).getTime()/1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 取得当前时间戳（精确到秒）
	 * @return
	 */
	public static String timeStamp(){
		long time = System.currentTimeMillis();
		String t = String.valueOf(time/1000);
		return t;
	}
	/**
	 * 返回当前时间前几天的时间格式
	 * @param simpleDateFormat
	 * @param index
	 * @return
	 */
	public  String getOldDay(String simpleDateFormat,int index){
		SimpleDateFormat fmt = new SimpleDateFormat(simpleDateFormat);
		Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
		calendar.set(Calendar.DAY_OF_MONTH,index); //当前时间减去一天，即一天前的时间
		String date = fmt.format(calendar.getTime());
		return date;
	}
}
